Skip to main content

🚀 NGINX Load Balancer Configuration

📌 Overview

This configuration sets up NGINX as a load balancer to distribute traffic among multiple backend servers.

🛠️ Configuration

http {
upstream backend {
server backend1.example.com:80; # 🖥️ Backend Server 1
server backend2.example.com:80; # 🖥️ Backend Server 2
server backend3.example.com:80; # 🖥️ Backend Server 3
}

server {
listen 80;
server_name example.com; # 🌎 Your Domain

location / {
proxy_pass http://backend; # 🔀 Load Balancing to Backends
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

✅ Testing the Load Balancer

1⃣ Check NGINX Configuration

Run the following command to ensure there are no syntax errors:

nginx -t

If everything is correct, you should see:

nginx: configuration file /etc/nginx/nginx.conf test is successful

2⃣ Restart NGINX

Apply the changes by restarting NGINX:

systemctl restart nginx  # For Linux (systemd)
service nginx restart # Alternative method

3⃣ Verify Load Balancing

Use curl or a browser to send requests:

curl -I http://example.com

Refresh multiple times to see different responses from backend servers.

4⃣ Check Logs

Monitor logs to verify traffic distribution:

tail -f /var/log/nginx/access.log

🚀 Your NGINX Load Balancer is now ready!